//app.js

App({
  onLaunch: function (path) {
    console.log(getCurrentPages())
   //授权获取openID
    wx.getStorage({ //判断storage 中有无openid
      key: 'openID',
      success: function (res) {
        if (res == '') {
          wx.login({
            success: function (res) {
              if (res.code) {
                // 发起网络请求 获取openID
                wx.request({
                  url: 'http://wx.hezi.weiyingjia.org/askforum/getopenid',
                  method: 'POST',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  dataType: 'json',
                  data: {
                    code: res.code
                  },
                  success: function (res) {
                   //将openID放入storage中
                    wx.setStorage({
                      key: "openID",
                      data: res.data.openid,
                      success: function (data) {

                      }
                    })
                  }
                })
              } else {
                console.log('获取用户登录态失败！' + res.errMsg)
              }
            }
          });
        }
      }
    });
    /**
     * 判断用户是否登录
     */
    wx.getStorage({ //判断storage 中有无userid 用户登录状态判断
      key: 'userid',
      success: function (res) {
  
      
      },
      fail:function(res){
        wx.redirectTo({
          url: 'register/register'
        })
      }
    })
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
  },
  getUserInfo:function(cb){
    var that = this
    if(this.globalData.userInfo){
      typeof cb == "function" && cb(this.globalData.userInfo)
    }else{
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  },
  /**
   * 定义全局常量
   */
  globalData:{
    userInfo:null,
    domain:'http://wx.hezi.weiyingjia.org',
    openid:''
  }
})